import axios from 'axios'
import store from '@/store'
import {
	Message
} from 'element-ui'
import router from '@/router'
import {
	getTimeStamp
} from '@/utils/auth'

const TimeOut = 3600

const service = axios.create({
	baseURL: process.env.VUE_APP_BASE_API,
	timeout: 10000
})

service.interceptors.request.use(config => {
	if (store.getters.token) {
		if (IsCheckTimeOut()) {
			store.dispatch('user/logout')
			router.push('/login')
			return Promise.reject(new Error('token超时了'))
		}
		config.headers['Authorization'] = `Bearer ${store.getters.token}`
	}
	return config
}, error => {
	return Promise.reject(error)
})

service.interceptors.response.use(response => {
	const {
		success,
		message,
		data
	} = response.data
	if (success) {
		return data
	} else {
		Message.error(message)
		return Promise.reject(new Error(message))
	}
}, error => {
	Message.error(error.message)
	return Promise.reject(error)
})

function IsCheckTimeOut() {
	var currentTime = Date.now()
	var timeStamp = getTimeStamp()
	return (currentTime - timeStamp) / 1000 > TimeOut
}

export default service
